package com.um2.mauction.java;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.DynaActionForm;

import com.um2.mauction.entity.Vente;
import com.um2.mauction.session.remote.AjoutProduitStatefulRemote;

public class FrmAjoutProduitParticulier2Action extends Action
{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
	{

		ActionForward resultat = mapping.findForward("success");
		ActionErrors errors = new ActionErrors();

		// Récuparation données du bean
		DynaActionForm formulaire = (DynaActionForm) form;

		String dateVente = (String) formulaire.get("txtAuctionDate");
		double dureeVente = Double.parseDouble((String) formulaire.get("txtAuctionDuration")); 
		float prixInitialVente = Float.parseFloat((String) formulaire.get("txtAuctionfirstPrice"));
		float prixReserveVente =Float.parseFloat((String) formulaire.get("txtAuctionWantedPrice")) ;

		//récupération Stateful bean
		HttpSession session = request.getSession();	
		AjoutProduitStatefulRemote ajoutprod=(AjoutProduitStatefulRemote)session.getAttribute("ajoutprod");

		if(ajoutprod==null)
		{
			errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("ajoutprod.statefulBean.null"));
			resultat = new ActionForward(mapping.getInput());
		}
		else
		{		
			System.out.println(ajoutprod.getProduit());

			if (ajoutprod.getProduit()==null)
			{
				errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("ajoutprod.phase1.produitIsNull"));
				resultat = new ActionForward(mapping.getInput());
			}
			else
			{
				Vente v = new Vente(prixInitialVente,prixReserveVente,dateVente,dureeVente);
				ajoutprod.setVente(v);
			}
		}
		if (!errors.isEmpty()) 
			saveErrors(request, errors);
		return resultat;
		//return mapping.findForward("success");
	}
}